package com.example.keepmesafe.dialogs;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Toast;

import com.example.keepmesafe.R;
import com.example.keepmesafe.activities.UserHome;
import com.example.keepmesafe.domain.Tag;
import com.example.keepmesafe.repository.DBHelper;
import com.example.keepmesafe.service.AESCrypto;
import com.example.keepmesafe.service.Security;

@SuppressLint("ValidFragment")
public class CustomOptionsDialog extends DialogFragment {
	private DBHelper tagStorageImpl;
	private Context ctx;
	private Tag tagToBeUsed;
	private Activity parentActivity;
	public CustomOptionsDialog(){}
	
	public CustomOptionsDialog(Activity parentActivity,Context ctx, Tag tag){
		this.ctx = ctx;
		tagStorageImpl = new DBHelper(ctx);
		this.tagToBeUsed = tag;
		this.parentActivity = parentActivity;
	}
	
	 @Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
		    final SQLiteDatabase db = tagStorageImpl.getWritableDatabase();
	        // Use the Builder class for convenient dialog construction
	        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	        builder.setMessage("Please Select An Option")
	               .setPositiveButton(R.string.btnDelete, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   final int isDeletedSuccess = tagStorageImpl.deleteTag(tagToBeUsed.getId(), db);
	                	   if(isDeletedSuccess!=1)
	                		   Toast.makeText(ctx, "The tag was not successfully deleted", Toast.LENGTH_SHORT).show();
	                	   else
	                		   Toast.makeText(ctx, "The tag was successfully deleted", Toast.LENGTH_SHORT).show();
	                	   
	                	   UserHome uh  = (UserHome)parentActivity;
	                	   uh.refreshList();
	                   }
	               })
	               .setNegativeButton(R.string.btnViewPassword, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   final AESCrypto crypto = new AESCrypto();
	                	   try {
							final String decryptedPassword = crypto.decrypt(tagToBeUsed.getTagPassword());
								Toast.makeText(ctx, "Password: " + decryptedPassword, Toast.LENGTH_SHORT).show();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
	                 	    UserHome uh  = (UserHome)parentActivity;
	                   	    uh.refreshList();
	                   }
	               });
	        // Create the AlertDialog object and return it
	        return builder.create();
	    }
	 
}
